package com.good.cm.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.good.cm.dao.PatientVisitInfoMapper;
import com.good.cm.dao.TriagePatientMapper;
import com.good.cm.dao.TriageRecordMapper;
import com.good.cm.exception.ParamsException;
import com.good.cm.model.ResultInfo;
import com.good.cm.model.TriageListQuery;
import com.good.cm.model.TriagePatientVisitModel;
import com.good.cm.utils.AssertUtil;
import com.good.cm.utils.RandomGUID;
import com.good.cm.vo.TriagePatient;
import com.good.cm.vo.TriagePatientVisitInfo;
import com.good.cm.vo.TriageRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PatientVisitService {
    @Resource
    private PatientVisitInfoMapper patDao;

    @Resource
    private TriagePatientMapper triagePatientMapper;
    @Resource
    private TriageRecordMapper triageRecordMapper;
    /**
     * 查询分诊全部信息-列表
     * @param query
     * @return
     */
    public List<TriagePatientVisitInfo> getPatientList(TriageListQuery query){
        return patDao.getListByDate(query);
    }


    /**
     * 通过pvid主键获取全部分诊信息
     * */
    public ResultInfo getPatientInfoByPvid(String pvid)
    {
        ResultInfo info=new ResultInfo();
        TriageListQuery query=new TriageListQuery(pvid);
        List<TriagePatientVisitInfo> patList= patDao.getListByDate(query);
        if (patList!=null && patList.size()>0)
        {
            info.setResult(patList.get(0));
        }else  {
            info.setCode(500);
            info.setMsg("通过pvid没有查询到患者信息");
        }
        return info;
    }
    /**
     * 保存分诊全部信息
     * @param model
     * @return
     */
    @Transactional //开启事务
    public ResultInfo SaveTriagePatientVisitModel(TriagePatientVisitModel model)
    {
        System.out.println("打印实体"+JSON.toJSONString(model));
        //1.保存基本信息 //2.保存记录表
        ResultInfo info=new ResultInfo();
        if(model ==null)
        {
            throw new ParamsException("患者为空");
        }
        if(StringUtils.isBlank(model.triagePatient.pvid))
        {   //1.基本信息添加
            model.triagePatient.pvid= RandomGUID.generatorGUID();
            model.triagePatient.status=2;
            model.triagePatient.visitdate=new Date(System.currentTimeMillis());
            int rc= triagePatientMapper.insert(model.triagePatient);
            if (rc<1)
            {
                throw new ParamsException("保存基本信息失败！");
            }
            //2.记录信息添加
            model.triageRecord.tid= RandomGUID.generatorGUID();
            model.triageRecord.pvid=model.triagePatient.pvid;
            model.triageRecord.triagedt=new Date(System.currentTimeMillis());
            int trc= triageRecordMapper.insert(model.triageRecord);
            if (trc<1)
            {
                throw new ParamsException("保存记录表失败！");
            }
        }else
        {   //1.基本信息修改
            UpdateWrapper<TriagePatient> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("pvid", model.triagePatient.pvid);
            int rc=   triagePatientMapper.update(model.triagePatient, updateWrapper);
            if (rc<1)
            {
                throw new ParamsException("保存记录表失败！");
            }
            if (AssertUtil.stringToInt(model.triageRecord.acttriagelevel)>5)
            {
                throw new ParamsException("分诊等级错误！");
            }

            //2.记录信息修改
            UpdateWrapper<TriageRecord> updateRecordWrapper = new UpdateWrapper<>();
            updateRecordWrapper.eq("tid", model.triageRecord.tid);
            int trc=   triageRecordMapper.update(model.triageRecord, updateRecordWrapper);
            if (trc<1)
            {
                throw new ParamsException("保存记录表失败！");
            }
        }
        return  info;
    }
}
